package com.zzl.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zzl.services.GradeServices;
import com.zzl.services.impl.GradeServicesImpl;
import com.zzl.util.DbUtil;
import com.zzl.util.JsonUtil;
import com.zzl.util.ResponseTypeUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;

public class GradeComboListServlet extends HttpServlet {
    DbUtil dbUtil=new DbUtil();
    GradeServices gradeService=new GradeServicesImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Connection con=null;
        try {
            con=dbUtil.getCon();
            JSONArray jsonArray=new JSONArray();
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("id","");
            jsonObject.put("gradeName","请选择...");
            jsonArray.add(jsonObject);
            jsonArray.addAll(JsonUtil.formatRsToJsonArray(gradeService.gradeList(con,null,null)));
            ResponseTypeUtil.write(resp,jsonArray);
        }catch (Exception e){
            e.printStackTrace();
        }
        finally {
            try {
                dbUtil.closeCon(con);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
